package com.tqjc.system.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tqjc.system.common.entity.BO.SysMaterialInfoBO;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.ResponseUtil;
import com.tqjc.system.system.entity.SysGateRecordEntity;
import com.tqjc.system.system.entity.SysMaterialInfoEntity;
import com.tqjc.system.system.model.bo.SysGateRecordBO;
import com.tqjc.system.system.service.SysMaterialManageService;
import com.tqjc.system.system.wrapper.GateRecordWrapper;
import com.tqjc.system.system.wrapper.MaterialManageWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 86195
 * @version 1.0
 * @description 物料管理控制层
 * @date 2023/11/7 14:10
 */
@Controller
@RequestMapping("/sys/material")
@Slf4j
public class SysMaterialManageController extends SysCommonController{


    @Autowired
    private SysMaterialManageService service;

    @RequestMapping("/index")
    public String index(Model model){
        return "sys/manage/material_index.html";
    }


    @RequestMapping("/addIndex")
    public String addIndex(Model model){
        return "sys/manage/addDetail.html";
    }

    @RequestMapping("/infoList")
    public void infoList(SysMaterialInfoBO sysMaterialInfoBO, PageInfo<SysMaterialInfoEntity> page, HttpServletResponse response){
        log.info("查询物料编码接口开始");
        IPage<SysMaterialInfoEntity> lstResult = service.getListByPage(page.getPageEntity(), sysMaterialInfoBO);
        ResponseUtil.write(response, new MaterialManageWrapper(lstResult).wrap());
        log.info("查询物料编码接口开始");

    }


    @RequestMapping("/query/id")
    public String queryDetailById(String id,Model model){
        log.info("根据id查询物料信息接口开始，id：{}",id);

        //结果处理,直接写到客户端
        SysMaterialInfoEntity sysMaterialInfo = service.queryById(id);
        model.addAttribute("sysMaterialInfo",sysMaterialInfo);
        log.info("根据id查询物料信息接口结束，id：{}",id);
        return "sys/manage/materialDetail.html";
    }



    @ResponseBody
    @RequestMapping("/update")
    public AjaxJson update(SysMaterialInfoBO sysMaterialInfoBO){
        try {
            log.info("编辑物料信息接口开始");

            service.updateInfo(sysMaterialInfoBO);
            log.info("编辑物料信息接口结束");

        } catch (Exception e) {
            return AjaxJson.fail("编辑失败");
        }
        return AjaxJson.ok("编辑成功");

    }



    @ResponseBody
    @RequestMapping("/add")
    public AjaxJson add(SysMaterialInfoBO sysMaterialInfoBO){
        try {
            log.info("编辑物料信息接口开始");

            service.addInfo(sysMaterialInfoBO);
            log.info("编辑物料信息接口结束");

        } catch (Exception e) {
            return AjaxJson.fail("编辑失败");
        }
        return AjaxJson.ok("编辑成功");

    }


}
